package uns.fit.gis.ui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.i18n.LocaleManager;

public class GPSConnectionList extends List  implements CommandListener {
	private Command backCommand;
	private Command selectCommand;
	private Command searchCommand;
	private MapDrawer midlet; 
	private int time;
	
	public GPSConnectionList(String title, MapDrawer midlet) {
		super(title, List.EXCLUSIVE);
		this.midlet = midlet;
		
		setCommandListener(this);
		backCommand = new Command(LocaleManager.getMessage("back"), Command.BACK, 1);
		addCommand(backCommand);
		selectCommand = new Command(LocaleManager.getMessage("select"), Command.OK, 1);
		addCommand(selectCommand);
		searchCommand = new Command(LocaleManager.getMessage("search"), Command.OK, 1);
		addCommand(searchCommand);
	}

	public void setDeviceNames(String[] deviceNames) {
		time = 10000; // first time search in 5 seconds
		deleteAll();
		if(deviceNames != null) {
			for(int i = 0; i < deviceNames.length; i ++) {
				append(deviceNames[i], null);
			}
		}
	}
	
	public void commandAction(Command c, Displayable d) {
		if(c == backCommand) {
			midlet.showMapCanvas();
		}
		else if(c == selectCommand) {
			System.out.println(getSelectedIndex());
			midlet.mapCanvas.startGPS(getSelectedIndex());
			midlet.showMapCanvas();
		}
		else if(c == searchCommand) {
			time += 10000;
			midlet.mapCanvas.searchGPS(time);
		}
	}
}
